from django.shortcuts import render,HttpResponse
from . import models
from Product.models import Product
from User.models import User
import json

# Create your views here.

def CollectListView(request):
    try:
        if(request.method == 'GET'):
            #获取前端返回数据
            json_result = json.loads(request.body.decode())
            uid = json_result.get('uid')
            user = User.objects.get(UID = uid)
            
            #查询用户的收藏商品
            items = models.Collect.objects.filter(UID = user)
            
            #返回结果
            results = json.loads(json.dumps({}))
            
            if (items.count()==0):
                return HttpResponse("Empty")
            else:
                #遍历结果
                for i in items:
                    #获取商品
                    product = i.PID
                    pid = product.PID
                    product_name = product.ProdName
                    product_image1 = product.ProdImage1
                    product_image2 = product.ProdImage2
                    product_image3 = product.ProdImage3
                    result = {
                        'product_name': product_name,
                        'product_image1': product_image1,
                        'product_image2': product_image2,
                        'product_image3': product_image3,
                    }
                    results[pid] = result
                    
                return HttpResponse(json.dumps(results,ensure_ascii=False),content_type="application/json,charset=utf-8") 
    except:
        return HttpResponse("Fail(id:0)")

def AddCollectView(request):
    try:
        if(request.method == 'POST'):
            #获取前端返回数据
            json_result = json.loads(request.body.decode())
            uid = json_result.get('uid')
            pid = json_result.get('pid')
            user = User.objects.get(UID = uid)
            product = Product.objects.get(PID = pid)
            models.Collect(UID = user, PID = product).save()
            return HttpResponse("Success")
    except:
        return HttpResponse("Fail(id:0)")
        
def DeleteCollectView(request):
    try:
        if(request.method == 'POST'):
            #获取前端返回数据
            json_result = json.loads(request.body.decode())
            uid = json_result.get('uid')
            pid = json_result.get('pid')
            user = User.objects.get(UID = uid)
            product = Product.objects.get(PID = pid)
            models.Collect.objects.filter(UID = user, PID = product).delete()
            return HttpResponse("Success")
    except:
        return HttpResponse("Fail(id:0)")


def CollectSetView(request):
    valid = False
    #try:
    if(request.method == 'POST'):
        #获取前端返回数据
        json_result = json.loads(request.body.decode())
        uid = json_result.get('uid')
        pid = json_result.get('pid')
        marked = json_result.get('marked')
        user = User.objects.get(UID = uid)
        product = Product.objects.get(PID = pid)
        if (marked):
            models.Collect(UID = user, PID = product).save()
        else:
            models.Collect.objects.filter(UID = user, PID = product).delete()
        valid = True
    #except:
    #    valid = False
        
    result = {
        'valid':valid,
    }
    return HttpResponse(json.dumps(result,ensure_ascii=False),content_type="application/json,charset=utf-8")